package archforce.atp.x.ssh.tool;

import archforce.atp.x.ssh.entity.JumpConfig;
import archforce.atp.x.ssh.entity.JumpMappingConfig;
import archforce.atp.x.ssh.entity.PortMappingEntity;
import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author wx
 * @version 1.0
 * @date 2022/4/27 11:25
 */
public class JumpMappingCmdTool {

    private final static String SPLIT = " ";
    private final static String IPPortSplit = ":";
    private final static String UserForHostSplit = "@";
    private final static String JumpHostSplit = ",";
    private final static String sshJumpParamChar = "-J";
    private final static String mappingParamChar = "-g -L";
    private final static String defaultLocalMappingIP = "localhost";
    private final static Integer defaultPort = 22;

    public static String buildSSHJumpCmd(JumpConfig jumpConfig){
        StringBuilder cmd = new StringBuilder();
        cmd.append("ssh").append(SPLIT).append(sshJumpParamChar).append(SPLIT);
        return cmd.append(buildJumpCmd(jumpConfig)).toString();
    }

    public static String buildJumpCmd(JumpConfig jumpConfig){
        final Map<String, String> password = new HashMap<>();
        String cmdStr = jumpConfig.getJumpHost().stream()
                .map(host -> {
                    StringBuilder cmd = new StringBuilder();
                    cmd.append(host.getUser()).append(UserForHostSplit).append(host.getHostIP());
                    return cmd;
                })
                .collect(Collectors.joining(JumpHostSplit));
        cmdStr += (SPLIT + jumpConfig.getDockerCMasterServer().getUser() + UserForHostSplit + jumpConfig.getDockerCMasterServer().getHostIP());
        return cmdStr;
    }

    public static String buildSSHWholeCmd(JumpMappingConfig jumpMappingConfig){
        StringBuilder cmd = new StringBuilder();
        cmd.append(buildSSHJumpCmd(jumpMappingConfig.getJumpConfig()));
        cmd.append(SPLIT);
        cmd.append(buildMappingCmd(jumpMappingConfig.getMappingPortConfig()));
        return cmd.toString();
    }

    public static String buildMappingCmd(List<PortMappingEntity> mappingConfigs){
        return mappingConfigs.stream()
                .map(mappingEntity ->{
                    StringBuilder mappingCmd = new StringBuilder();
                    String local = (StringUtils.isEmpty(mappingEntity.getLocalhost())?defaultLocalMappingIP:mappingEntity.getLocalhost());
                    Integer localPort = (mappingEntity.getLocalPort() == null ? defaultPort : mappingEntity.getLocalPort());
                    Integer remotePort = (mappingEntity.getRemotePort() == null ? defaultPort : mappingEntity.getRemotePort());
                    mappingCmd.append(mappingParamChar).append(SPLIT);
                    mappingCmd.append(local).append(IPPortSplit).append(localPort);
                    mappingCmd.append(IPPortSplit);
                    mappingCmd.append(mappingEntity.getRemoteHost()).append(IPPortSplit).append(remotePort);
                    return mappingCmd;
                })
                .collect(Collectors.joining(SPLIT));
    }
}
